package com.iot.gateway.utils.serializer;

public class TopicSerializer<T> implements DataSerializer<T> {

    private final String topicTemplate;

    public TopicSerializer(String topicTemplate) {
        this.topicTemplate = topicTemplate;
    }

    @Override
    public String serialize(T data) throws Exception {
        String formatted = topicTemplate;
        // 替换模板中的占位符（示例实现）
        for (java.lang.reflect.Field field : data.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            String placeholder = "{" + field.getName() + "}";
            if (formatted.contains(placeholder)) {
                Object value = field.get(data);
                formatted = formatted.replace(placeholder, value != null ? value.toString() : "");
            }
        }
        return formatted;
    }

}
